#ifndef GMULTIGLOBALOBJECTMANAGER_INC
#define GMULTIGLOBALOBJECTMANAGER_INC


#include "GObjectBase.h"
#include "Common/Compat.h"

#include <unordered_map>
#include "wch/Mutex.h"

template <typename _VT, typename _VK >
class GMultiGlobalObjectManagerT
{
public:
	void add(const _VK& key, _VT* val)
	{
		wch::Mutex::ScopedLock lk(_objMutex);
		_objs.insert( std::make_pair( key, val ) );
	}
	template <typename _VK2>
	_VT * find( const _VK& key, const _VK2& key2 )
	{
		wch::Mutex::ScopedLock lk( _objMutex );
		typedef typename std::unordered_multimap<_VK, _VT * >::iterator find_itr;
		std::pair<find_itr, find_itr> range = _objs.equal_range( key );
		for ( find_itr itr = range.first; itr != range.second; ++ itr )
		{
			if ( itr->second->getId() == key2 )
				return itr->second;
		}
		return nullptr;
	}
	inline size_t size()
	{
		wch::Mutex::ScopedLock lk(_objMutex);
		return _objs.size();
	}

protected:
	std::unordered_multimap<_VK, _VT * > _objs;
	wch::Mutex _objMutex;
};

#endif
